Skip to content

Fix #836: correct _const_node_type_names fallback for Python 3.14+ - #837

Closed
EhteshamSid wants to merge 1 commit into
python-rope:masterfrom
EhteshamSid:fix/py314-const-node-type-names
Closed

EhteshamSid wants to merge 1 commit into
python-rope:masterfrom
EhteshamSid:fix/py314-const-node-type-names

Conversation

@EhteshamSid

Copy link
Copy Markdown

Fixes #836.

ast._const_node_type_names was removed in Python 3.14. The except ImportError fallback in rope/base/ast.py already handled this case at the assertion level, but the fallback dict still used the old pre-3.8 names ("Num", "Str", "NameConstant", etc.). On Python 3.14 all constant literals are ast.Constant nodes, so the fallback dict should map every type to "Constant" instead.

Split the except block into two branches: >= (3, 8) (i.e. Python 3.14+ which removed the attribute) uses "Constant" for all types; < (3, 8) keeps the original mapping for the old node types. Also removed the bare assert since the if/else now makes the intent explicit.

Added ConstNodeTypeNamesTest to cover the expected return values of get_const_subtype_name() per Python version.

marlon-costa-dc added a commit to marlon-costa-dc/rope that referenced this pull request Sep 25, 2026
Fork of upstream 1.14.0 with the datacosmos integration: PEP 695 type
parameters, full signature tokens, f-string delimiter fixes and the
adopted upstream PRs python-rope#808, python-rope#837, python-rope#858, python-rope#865, python-rope#867, python-rope#868, python-rope#874, python-rope#880.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mcepl

mcepl commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Before merging this, we need to clarify the contract of get_const_subtype_name().

Its docstring says "Get pre-3.8 ast node name", and get_node_type_name() delegates to it for Constant nodes. Returning "Constant" for every value on Python 3.14 therefore changes the helper's semantics; the removal of a private stdlib mapping does not by itself establish that this is the right replacement.

My preference is to preserve the historical subtype names across supported Python versions unless a caller demonstrably needs a different result.

Could you add a regression showing the user-visible failure on Python 3.14 and trace which consumer requires the changed names? Please also cover all supported constant value types. If the helper's contract genuinely needs to change, we should make that explicit and update its documentation and consumers together.

@mcepl
mcepl requested review from lieryan and mcepl and removed request for lieryan October 26, 2026 21:15
@mcepl

mcepl commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Could you trace which Rope consumer requires get_const_subtype_name() to return "Constant" on Python 3.14 and add a regression demonstrating the user-visible failure without this change? Please cover the supported constant value types. If no consumer needs the new name, I suggest preserving the documented historical subtype names.

@lieryan

lieryan commented Nov 3, 2026

Copy link
Copy Markdown
Member

The rope.base.ast module is not a public facing API. It's not a documented API and there's zero reasons consumers should be reaching to this level.

These functions were added to help with smoothing over the Python 3.8 ast changes in patchedasttest.py and that's the only place it has ever been used in the rope codebase.

Now that Python 3.8 is well out of support, this is just dead weight that can be removed as far as I'm concerned.

If someone did actually call into this API, it's simple enough for them to back port it themselves to their own codebase.

@mcepl

mcepl commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

The import failure reported in #836 was already fixed by a083c5d and released in Rope 1.14.0. The report concerned Rope 1.13.0; #836 has since been closed as a duplicate of #810.

I also checked this PR's head, b0430d7, on Python 3.14.8. The proposed mapping change breaks 69 patched-AST tests because their checker still expects historical names such as Num, Str and NameConstant. For example:

Expected <Num> but was <Constant>
Expected <NameConstant> but was <Constant>

Running ropetest.refactor.patchedasttest through unittest gave:

  • Current master: 175 tests, 0 failures, 0 errors, 6 skipped.
  • This PR: 149 tests, 69 failures, 0 errors, 6 skipped.

The different test totals reflect the older PR branch. These runs used Python 3.14.8 with the installed pure-Python dependencies made available on its import path.

I agree with @lieryan that these helpers do not need to be preserved as a public compatibility contract. My earlier emphasis on preserving that contract was misplaced. However, removing the obsolete helpers should update the test checker and assertions at the same time, consistently across supported Python versions, rather than changing the names only on Python 3.14 and leaving the callers unchanged.

Since the reported import failure is already fixed, I suggest closing this PR as superseded and handling removal of the compatibility helpers as a separate cleanup.

@mcepl mcepl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just close this PR per my last comment

@lieryan lieryan closed this Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImportError: cannot import name '_const_node_type_names' from 'ast' (/usr/lib/python3.14/ast.py)

3 participants